#include "hud_text.h"

hudText *hudText::createHUDText(node* parent,const char* name, const char* text, glm::vec2 render_pos, glm::vec2 size, char* fontPath, 
        int fontSize, const char* bgPath,Anchor anchor)
{
    auto hudText_ = new hudText();
    hudText_->init();
    hudText_->name_ = name;
    hudText_->setRenderPosition(render_pos);
    hudText_->setBg(sprite::createSprite(hudText_,bgPath,1.0f,anchor));
    hudText_->setSize(size);
    hudText_->setTextLabel(textLabel::createTextLabel(hudText_,text,fontPath,fontSize,anchor));
    if(parent){
        parent->addChild(hudText_);
    }
    return hudText_;
}

void hudText::setBackground(const char *path)
{
    if(bg){
        bg->setData(std::string(path));
    }else{
        bg = sprite::createSprite(this,path);
    }
}

void hudText::setSize(glm::vec2 size_)
{
    size = size_;
    bg ->setSize(size);
}

void hudText::setBgSizeByText(float margin)
{
    auto text_size = label->getSize();
    setSize(text_size + margin * 2);
}
